using System;
using System.Collections.Generic;
using System.IO;
using Common;
using Common.Enum;
using SProject.Program;

namespace SProject
{
	public class PCRProgramManage
	{
		private static PCRProgramManage _PCRProgramManage;

		public Dictionary<EProjectType, PCRProgram> AllProgramList { get; private set; }

		public static PCRProgramManage GetInstance()
		{
			if (_PCRProgramManage == null)
			{
				_PCRProgramManage = new PCRProgramManage();
			}
			return _PCRProgramManage;
		}

		private PCRProgramManage()
		{
			InitProgramList();
		}

		public void InitProgramList()
		{
			AllProgramList = new Dictionary<EProjectType, PCRProgram>();
			Utility.GetPCRProgramPath();
			foreach (KeyValuePair item in EnumManagement.GetAllProjectType())
			{
				EProjectType eProjectType = (EProjectType)item.Key;
				PCRProgram pCRProgram = LoadPCRProgram(eProjectType);
				if (pCRProgram.Programs.Count == 0)
				{
					setDefaultProgram(eProjectType, pCRProgram);
				}
				AllProgramList.Add(eProjectType, pCRProgram);
			}
		}

		private PCRProgram LoadPCRProgram(EProjectType Type)
		{
			PCRProgram pCRProgram = new PCRProgram();
			string savePath = getSavePath(Type);
			if (File.Exists(savePath))
			{
				FileStream fileStream = null;
				StreamEx streamEx = new StreamEx();
				try
				{
					fileStream = new FileStream(savePath, FileMode.Open, FileAccess.Read);
					fileStream.CopyTo(streamEx);
					streamEx.Seek(0L, SeekOrigin.Begin);
					pCRProgram.ReadFromStream(streamEx);
				}
				catch (Exception)
				{
				}
				finally
				{
					if (streamEx != null)
					{
						streamEx.Close();
					}
					if (fileStream != null)
					{
						fileStream.Close();
					}
				}
			}
			return pCRProgram;
		}

		private void setDefaultProgram(EProjectType Type, PCRProgram pcr)
		{
			switch (Type)
			{
			case EProjectType.Melt:
			{
				MeltingSegment meltingSegment2 = new MeltingSegment();
				meltingSegment2.IsHRM = false;
				meltingSegment2.StartTemp = 60.0;
				meltingSegment2.EndTemp = 90.0;
				meltingSegment2.Rate = 0.03;
				Segment segment3 = new Segment();
				Step step2 = new Step();
				step2.HoldMinute = 2;
				step2.HoldSecond = 0;
				step2.Temperature = 60.0;
				segment3.Items.Add(step2);
				segment3.Cycles = 1;
				pcr.Programs.Add(segment3);
				pcr.Programs.Add(meltingSegment2);
				break;
			}
			case EProjectType.HRM:
			{
				MeltingSegment meltingSegment = new MeltingSegment();
				meltingSegment.ScanMode = MeltingSegment.SCAN_CONTINUE;
				meltingSegment.Rate = 0.01;
				meltingSegment.Interval = 0.2;
				meltingSegment.HoldTime = 10;
				meltingSegment.StartTemp = 60.0;
				meltingSegment.EndTemp = 90.0;
				meltingSegment.IsHRM = true;
				pcr.Programs.Add(meltingSegment);
				break;
			}
			case EProjectType.AQ:
			case EProjectType.TQ:
			{
				Segment segment2 = new Segment();
				Step step = new Step();
				step.HoldMinute = 2;
				step.HoldSecond = 0;
				step.Temperature = 50.0;
				segment2.Items.Add(step);
				step = new Step();
				step.HoldMinute = 5;
				step.HoldSecond = 0;
				step.Temperature = 95.0;
				segment2.Items.Add(step);
				pcr.Programs.Add(segment2);
				segment2 = new Segment();
				step = new Step();
				step.HoldSecond = 15;
				step.Temperature = 95.0;
				segment2.Items.Add(step);
				step = new Step();
				step.HoldSecond = 40;
				step.Temperature = 60.0;
				step.IsAcquisition = true;
				segment2.Items.Add(step);
				segment2.Cycles = 40;
				pcr.Programs.Add(segment2);
				break;
			}
			case EProjectType.IA:
			{
				pcr.Programs.Add(new PreHeatSegment());
				Segment segment = new Segment();
				segment.Items.Add(new Step());
				pcr.Programs.Add(segment);
				break;
			}
			default:
			{
				Segment segment = new Segment();
				segment.Items.Add(new Step());
				pcr.Programs.Add(segment);
				break;
			}
			}
		}

		private string getSavePath(EProjectType Type)
		{
			return Utility.GetPCRProgramPath() + Type.ToString() + ".program";
		}

		public bool SavePCRProgram(EProjectType Type, PCRProgram pcr)
		{
			bool result = false;
			string savePath = getSavePath(Type);
			StreamEx streamEx = new StreamEx();
			FileStream fileStream = null;
			try
			{
				pcr.SaveToStream(streamEx);
				streamEx.Seek(0L, SeekOrigin.Begin);
				fileStream = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
				streamEx.CopyTo(fileStream);
				AllProgramList[Type] = pcr;
				result = true;
			}
			catch (Exception)
			{
			}
			finally
			{
				if (streamEx != null)
				{
					streamEx.Close();
				}
				if (fileStream != null)
				{
					fileStream.Close();
				}
			}
			return result;
		}
	}
}
